package com.iterator.controller.web.systemManager;

import com.iterator.bean.web.dhy.RoleAddPermissionsBO;
import com.iterator.bean.web.dhy.RolePermissionsVO;
import com.iterator.bean.web.pojo.BaseRespVo;
import com.iterator.bean.web.dhy.RoleListVO;
import com.iterator.bean.web.dhy.RoleVO;
import com.iterator.service.web.systemManager.SystemManageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: DHY
 * @Date: 2021/1/9 21:33
 * @Description: 系统管理,角色管理
 */
@RestController
@RequestMapping("admin/role")
public class RoleController {
    @Autowired
    SystemManageService systemManageService;

    /**
     * 创建角色
     * @param map
     * @return
     */
    @PostMapping("create")
    @RequiresPermissions("admin:role:create")
    public BaseRespVo roleCreate(@RequestBody Map<String,String> map) {
        String desc = map.get("desc");
        String name = map.get("name");
        RoleVO role= systemManageService.roleCreate(desc,name);
        return BaseRespVo.ok(role);

    }

    /**
     * 角色管理页面显示
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("list")
    @RequiresPermissions("admin:role:list")
    public BaseRespVo showList(Integer page,Integer limit,String sort,String order,String name) {
        RoleListVO roleListVO= systemManageService.showList(page,limit,sort,order,name);
        return BaseRespVo.ok(roleListVO);

    }

    /**
     * 删除角色
     * @param map
     * @return
     */
    @PostMapping("delete")
    @RequiresPermissions("admin:role:delete")
    public BaseRespVo roleDelete(@RequestBody Map map) {
        Integer id = (Integer) map.get("id");
        systemManageService.roleDelete(id);
        return BaseRespVo.ok();
    }
    @PostMapping("update")
    @RequiresPermissions("admin:role:update")
    public BaseRespVo roleUpdate(@RequestBody Map map) {
        Integer id = (Integer) map.get("id");
        String name = (String) map.get("name");
        String desc = (String) map.get("desc");
        Date updateTime=new Date();
        systemManageService.roleUpdate(id,name,desc,updateTime);
        return BaseRespVo.ok();

    }
    /**
     * 授权条目显示
     * @param
     * @return
     */
    @GetMapping("permissions")
    @RequiresPermissions("admin:role:permissions:get")
    public BaseRespVo rolePermissions(Integer roleId) {
        RolePermissionsVO permissionsVO= systemManageService.rolePerMissions(roleId);
        return BaseRespVo.ok(permissionsVO);
    }

    /**
     * 添加角色权限
     * @return
     */
    @PostMapping("permissions")
    @RequiresPermissions("admin:role:permissions:update")
    public BaseRespVo addRolePerMissions(@RequestBody RoleAddPermissionsBO roleAddPermissionsBO) {
        systemManageService.roleAddPerMissions(roleAddPermissionsBO);
        return BaseRespVo.ok();
    }


    /**
     * 角色详情
     * @return
     */
    @GetMapping("options")
    @RequiresPermissions("admin:role:read")
    public BaseRespVo roleOptions() {
        List<Map> list= systemManageService.roleOptions();
        return BaseRespVo.ok(list);
    }
}
